<?php

namespace app\admin\controller\choi;

use app\admin\model\choi\Setting as SettingModel;
use app\common\controller\Backend;
use think\Cache;
use think\Db;

/**
 * 
 *
 * @icon fa fa-circle-o
 */
class Setting extends Backend
{
    
    /**
     * Setting模型对象
     * @var \app\admin\model\choi\Setting
     */
    protected $model = null;


    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\choi\Setting;

    }
    
    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */

    /**
     * 可语品牌&团队
     */
    public function team()
    {
        $this->createOrUpdate(['team', 'team_en']);
        return $this->fetch();
    }


    /**
     * 可语平台概况
     */
    public function situation()
    {
        $this->createOrUpdate(['situation', 'situation_en']);
        return $this->fetch();
    }

    /**
     * 可语平台概况
     */
    public function join()
    {
        $this->createOrUpdate(['join', 'join_en']);
        return $this->fetch();
    }

    /**
     * 可语平台概况
     */
    public function teacher()
    {
        $this->createOrUpdate(['teacher', 'teacher_en']);
        return $this->fetch();
    }

    /**
     * 客服热线 & 地址
     */
    public function address()
    {
        $this->createOrUpdate([
            'service_mobile',
            'mobile_use',
            'address',
            'address_en',
            'email',
            'email_use',
            'record',
            'record_en',
            'record_use',
            'xina'
        ]);
        return $this->fetch();
    }

    /**
     * 学生端说明
     */
    public function studentExplain()
    {
        $this->createOrUpdate(['student_explain', 'student_explain_en']);
        return $this->fetch();
    }

    /**
     * 教师端说明
     */
    public function teacherExplain()
    {
        $this->createOrUpdate(['teacher_explain', 'teacher_explain_en']);
        return $this->fetch();
    }


    /**
     * 添加 & 更新
     * @param array $param
     */
    private function createOrUpdate(array $params)
    {
        $rows = $this->data($params);
        $keys = SettingModel::getAllKeys();
        if ($this->request->isPost()) {
            $rows = $this->request->post('row/a');
            $describes = $this->request->post('describe/a');
            foreach ($rows as $k=>$v) {
                if (in_array($k, $keys)) {
                    $val = SettingModel::getValue($k);
                    if ($v != $val) {
                        $res = SettingModel::updateValue($k, $v);
                    } else {
                        $res = true;
                    }
                } else {
                    $res = SettingModel::add($k, $v, $describes[$k]);
                }
                Cache::set('setting_'.$k, $v, 0);
            }

            $res ? $this->success() : $this->error();
        }

        $this->assign('rows', $rows);
    }

    private function data(array $params) {
        $data = [];
        foreach ($params as $val) {
            $data[$val] = SettingModel::getValue($val);
        }
        return $data;
    }
}
